﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using RestSemitaService.Models;

namespace RestSemitaService.Controllers
{
    public class CustomerInvoiceController : ApiController
    {
        // GET api/customerinvoice
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        public string Get(string customer_id, string target_date)
        {
            try
            {
                SqlConnection thisConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
                thisConnection.Open();
                SqlCommand thisCommand = thisConnection.CreateCommand();
                thisCommand.CommandText = @"select 
                invoice_header.invoice_no invoice_no, invoice_header.customer_id customer_id, 
                (customer.first_name + ' ' + customer.last_name)  custname, invoice_header.target_date deliverDate , 
                invoice_amount amount
                from invoice_header join customer on invoice_header.customer_id = customer.customer_id 
                where invoice_header.customer_id = @customer_id and invoice_header.target_date = CONVERT(VARCHAR(10),CURRENT_TIMESTAMP,126)";
                thisCommand.Parameters.AddWithValue("@customer_id", customer_id);
                thisCommand.Parameters.AddWithValue("@target_date", target_date);
                using (SqlDataReader reader = thisCommand.ExecuteReader())
                {
                    List<InvoiceHead> invoiceHeads = new List<InvoiceHead>();
                    while (reader.Read())
                    {
                        string _invoice_no = reader["invoice_no"].ToString();
                        string _customer_id = reader["customer_id"].ToString();
                        string _custname = reader["custname"].ToString();
                        string _deliverDate = reader["deliverDate"].ToString();
                        string _amount = reader["amount"].ToString();
                        invoiceHeads.Add(new InvoiceHead
                        {
                            InvoiceNo = _invoice_no,
                            CustId = _customer_id,
                            CustName = _custname,
                            DeliveryDate = _deliverDate,
                            Amount = _amount
                        });
                    }
                    reader.Close();
                    thisConnection.Close();
                    string json = JsonConvert.SerializeObject(new
                    {
                        InvoiceHead = invoiceHeads
                    });
                    return json;
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.Message);
                return e.StackTrace + " \n >>>>>>>>>>>>>>>>> \n" + e.Message;
            }
        }

        // POST api/customerinvoice
        public void Post([FromBody]string value)
        {
        }

        // PUT api/customerinvoice/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/customerinvoice/5
        public void Delete(int id)
        {
        }
    }
}
